/**
 *签名。
 * <pre>
 * 作者：hugh zhuang
 * 邮箱：3378340995@qq.com
 * 日期：2016-02-26 15:07:33
 * 版权：苏州东大金点物联科技有限公司
 * </pre>
 * <pre>
 *    	 <div  class="fr-signature-outer" > 
 *  				<div class="fr-signature-header">
 *  					<div class="fr-signature-fieldtext">请在这里输入你的签名</div>
 *   				<div class="fr-signature-clear">
 *   					<span><a href="javascript:void(0);" data-toggle="signature-clear">清除</a></span>
 *   				</div>
 *   			</div> 
 *   			<textarea data-control="signature"   name="m:urlForm:signature" style="display: none;">${fn:escapeXml(urlForm.signature)}</textarea>
 *   			<div  class="fr-signature-div"  data-toggle="signature"  data-name="m:urlForm:signature"></div>
 *			 </div> 
 * </pre>
 */
$(function(){
	signatureControl = new SignatureControl();
	signatureControl.init();  //初始化操作
});

(function() {
	

	SignatureControl = function() {
		//定义属性
	};

	/**
	 * 方法
	 */
	SignatureControl.prototype = {
			/**
			 * 初始化
			 */
			init : function() {
				if (this.hasInit) // 是否已初始化
					return false;
				this.hasInit = true;
	
				this.initData();
			},
			initData:function(parent){
				var me = this;
				if(	$.isEmpty(parent))
					parent = $("[data-toggle='signature']");
				parent.each(function(){
		    	  	var $this=$(this),signature,
		    	  		$name = $("[name='"+ $this.data("name")+"']"),
		    	  		id = $this.attr("id");
		    	try {
		    		 $this.jSignature({
	            		signatureLine:true,
	            		lineWidth:0.5,
	            		width:'100%',
	            		height:$this.data("height")?$this.data("height"):150
	            	});
		    		 
		    		if($.isNotEmpty($name.val())){
		    			$name.jSignature("setData","data:"+$name.val());
	            	}
		    		
		    		$this.closest('.fr-signature-outer').find("[data-toggle='signature-clear']")
		    		.off("click").on('click',function(e){ 
		    			$this.jSignature('clear');
		    			$name.val('');
		    		});
		    		
		    		// 值改变 赋值给对象
		    		$this.off("change").on('change', function(e){ 
            			var val ='',
            				data = $(e.target).jSignature('getData', 'image');
            			if (typeof data === 'string'){
            				val = data;
            			} else if($.isArray(data) && data.length === 2){
            				val = data.join(',')
            			} else {
            				try {
            					val = JSON.stringify(data);
            				} catch (ex) {
            					console.info(ex)
            				}
            			}
            			$name.val(val);
            		});
		             
		    	} catch (e) {
				}
		    	});
			}
	}
})();